package gov.pbc.nn.kjgl.jfcr.handler;

import gov.pbc.nn.core.utils.SecurityUtils;
import gov.pbc.nn.kjgl.Constants;
import gov.pbc.nn.kjgl.entity.ComputerRoomApplyEntry;
import gov.pbc.nn.kjgl.jfcr.dao.ApplyEntryDao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.uflo.env.Context;
import com.bstek.uflo.model.ProcessInstance;
import com.bstek.uflo.model.task.Task;
import com.bstek.uflo.process.listener.TaskListener;
import com.bstek.uflo.process.node.TaskNode;

/**
 * 集成父类的监听时间，添加合适的方法
 * 
 * @author jack.zhao
 * 
 */
@Component("jfcr.SupplementTaskListener")
public class SupplementTaskListener implements TaskListener {
	@Autowired
	private ApplyEntryDao applyEntryDao;

	/**
	 * 返回true是直接跳过该节点，直接进入下一个节点，返回false则相反
	 * 判断流程变量中是否还有shenhren变量是否还有值，如果没有值就直接跳过到下一个节点
	 */
	@Override
	public boolean beforeTaskCreate(Context context, ProcessInstance processinstance, TaskNode tasknode) {
		@SuppressWarnings("unchecked")
		List<String> users = (List<String>) context.getProcessService().getProcessVariable("shenheren", processinstance);
		return users.isEmpty();
	}

	@Override
	public void onTaskComplete(Context arg0, Task arg1) {

	}

	@Override
	public void onTaskCreate(Context context, Task task) {
		String businessId = task.getBusinessId();
		ComputerRoomApplyEntry computerRoomApplyEntry = this.applyEntryDao.get(businessId, ComputerRoomApplyEntry.class);
		String assignee = task.getAssignee();
		boolean kz = SecurityUtils.containsRole(assignee, Constants.ROLE_KZ);
		boolean cz = SecurityUtils.containsRole(assignee, Constants.ROLE_CZ);
		boolean jg = SecurityUtils.containsRole(assignee, Constants.ROLE_JFGLY);
		if (kz) {
			task.setTaskName("科长补录【"+computerRoomApplyEntry.getTitle()+"】的审批信息");
//			task.setTaskName("科长补录审批信息");
		} else if (cz) {
			task.setTaskName("处长补录【"+computerRoomApplyEntry.getTitle()+"】的审批信息");
//			task.setTaskName("处长补录审批信息");
		} else if (jg) {
//			task.setTaskName("机房管理员补录审批信息");
			task.setTaskName("机房管理员补录【"+computerRoomApplyEntry.getTitle()+"】的审批信息");
		}
		context.getSession().saveOrUpdate(task);
	}

}
